# 公共类定义

from tornado.web import RequestHandler
from tornado import gen
from tornado.concurrent import run_on_executor
from concurrent.futures.thread import ThreadPoolExecutor
from time import strftime

from config.config import configs


config = configs()


# 声明基类 继承于 tornado.web.RequestHandler
class BaseHandler(RequestHandler):
    executor = ThreadPoolExecutor(config.threadNum)    # 多线程


    @run_on_executor
    def set_default_headers(self):
        # 访问权限设置
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Headers", "x-requested-with")
        self.set_header('Access-Control-Allow-Methods', 'POST')


    @run_on_executor
    def get(self):
        pass


    @run_on_executor
    def post(self):
        pass


# SQL数据处理
class SQLDealHandler():
    def __init__(self):
        super().__init__()

    class Index():
        def __init__(self):
            super().__init__()
            self.time = lambda dateTime: strftime("%Y-%m-%d_%H:%M:%S_%A")   # 年-月-日_时-分-秒_星期 ( _ 为分隔符)
        
        def sqlToJson(self, sqlData:list):
            data = {}

            for i in range(0, len(sqlData)):
                time = self.time(sqlData[i][9].timetuple())
                mob = {i:{
                    "headImg":sqlData[i][1],
                    "userName":sqlData[i][2],
                    "userSign":sqlData[i][3],
                    "mediaAddr":sqlData[i][4],
                    "cardTitle":sqlData[i][5],
                    "cardSubtitle":sqlData[i][6],
                    "cardContent":sqlData[i][7],
                    "likeNum":sqlData[i][8],
                    "time":time,
                    "atUser":sqlData[i][10]
                }}
                data.update(mob)
                
            return data